package com.jxc.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jxc.dao.BaseDaoI;
import com.jxc.model.po.Discount;
import com.jxc.model.po.Repertory;
import com.jxc.model.po.User;
import com.jxc.model.vo.DataGrid;
import com.jxc.model.vo.VDiscount;
import com.jxc.service.DiscountServiceI;

@Service("discountService")
public class DiscountServiceImpl implements DiscountServiceI {

	@Autowired
	private BaseDaoI<Discount> discountDao;
	@Autowired
	private BaseDaoI<User> userDao;
	@Override
	public List<VDiscount> combobox(VDiscount vdiscount) {
		return this.changeModel(this.findCombobox(vdiscount));
	}

	private List<Discount> findCombobox(VDiscount vdiscount) {
		String hql = "from Discount t where 1=1 ";
		List<Object> values = new ArrayList<Object>();
		hql = addWhere(vdiscount, hql, values);
		return discountDao.find(hql, values);
	}

	@Override
	public DataGrid datagrid(VDiscount vdiscount) {
		try {
			DataGrid dg = new DataGrid();
			dg.setRows(this.changeModel(this.find(vdiscount)));
			dg.setTotal(this.total(vdiscount));
			return dg;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public void delete(String ids) {
		if (ids != null) {
			for (String id : ids.split(",")) {
				if (!id.trim().equals("0")) {
					Discount discount = discountDao.get(Discount.class, Long.parseLong(id.trim()));
					if (discount != null) {
						discountDao.delete(discount);
					}
				}
			}
		}
	}

	@Override
	public void save(VDiscount vdiscount) {
		Discount discount = new Discount();
		BeanUtils.copyProperties(vdiscount, discount);
		System.out.println(vdiscount.getUid());
		if(vdiscount.getUid()!=null){
			User u  = userDao.get(User.class,vdiscount.getUid());
			discount.setUser(u);
		}
		discount.setIsDelete(false);
		this.discountDao.save(discount);
	}

	@Override
	public void update(VDiscount vdiscount) {
		Discount discount = discountDao.get(Discount.class, vdiscount.getId());
		BeanUtils.copyProperties(vdiscount, discount, new String[] { "id", "isDelete" });
	
		this.discountDao.update(discount);
	}

	private List<VDiscount> changeModel(List<Discount> discounts) {
		List<VDiscount> vdiscounts = new ArrayList<VDiscount>();
		if (discounts != null && discounts.size() > 0) {
			for (Discount discount : discounts) {
				VDiscount vdiscount = new VDiscount();
				BeanUtils.copyProperties(discount, vdiscount);
				vdiscounts.add(vdiscount);
			}
		}
		return vdiscounts;
	}

	private List<Discount> find(VDiscount vdiscount) {
		String hql = "from Discount t where 1=1 ";

		List<Object> values = new ArrayList<Object>();
		hql = addWhere(vdiscount, hql, values);

		if (vdiscount.getSort() != null && vdiscount.getOrder() != null) {
			hql += " order by " + vdiscount.getSort() + " " + vdiscount.getOrder();
		}
		return discountDao.find(hql, values, vdiscount.getPage(), vdiscount.getRows());
	}

	private Long total(VDiscount vdiscount) {
		String hql = "select count(*) from Discount t where 1=1 ";
		List<Object> values = new ArrayList<Object>();
		hql = addWhere(vdiscount, hql, values);
		return discountDao.count(hql, values);
	}

	private String addWhere(VDiscount vdiscount, String hql, List<Object> values) {
		if (vdiscount.getDiscountNum() != null) {
			hql += " and t.discountNum = ? ";
			values.add(vdiscount.getDiscountNum());
		}
		if (vdiscount.getDesc()!= null &&!vdiscount.getDesc().equals("")) {
			System.out.println(vdiscount.getDesc());
			hql += " and t.desc  like ? ";
			values.add("%%"+vdiscount.getDesc()+"%%");
		}if (vdiscount.getUid() != null) {
			hql += " and t.user.id  = ? ";
			values.add(vdiscount.getUid());
		}
		hql += " and t.isDelete = 0 ";
		return hql;
	}

}
